package com.ddstation.crm.login.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 校验工具(字符或数字)
 */
public class ValidateUtil {

	/**
	 * 校验手机号
	 * 
	 * @param mobile
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isMobile(String mobile) {
		/**
		 * 正则表达式：验证手机号
		 */
		// public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
		String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(16[0-9])|(17[0-9])|(18[0-9]))\\d{8}$";
		return Pattern.matches(REGEX_MOBILE, mobile);
	}

	public static boolean isEmpty(Object obj){
		if(obj instanceof String){
			return (obj == null || "".equals(obj));
		}
		return (obj == null);
	}

	public static boolean isLen(String str, int expectedLen) {
		return (str != null && str.length()==expectedLen);
	}

	public static boolean isLen(String str, int max, int min) {
		return (str != null && str.length()>=min && str.length()<=max);
	}

	public static boolean isValue(long value, long maxValue, long minValue) {
		return (value>=minValue && value<=maxValue);
	}

	public static boolean contains(String expected, String src) {
		return (src != null && expected.contains(src));
	}

	public static boolean contains(String[] expected, String src) {
		for(int i=0; i<expected.length; i++){
			if(expected[i].equals(src)) return true;
		}
		return false;
	}

	public static boolean eq(String src, String expected) {
		return (src != null && expected.equals(src));
	}

	public static boolean isLong(String value) {
		try {
			Long lCheckValue = Long.parseLong(value);
			if (lCheckValue instanceof Long == false) {
				return false;
			}
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
	
	public static boolean isInteger(String value) {
		try {
			Integer iCheckValue = Integer.parseInt(value);
			if (iCheckValue instanceof Integer == false) {
				return false;
			}
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
	
	public static boolean isFloat(String value) {
		try {
			Float fCheckValue = Float.parseFloat(value);
			if (fCheckValue instanceof Float == false) {
				return false;
			}
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
	
	public static boolean isDouble(String value) {
		try {
			Double dCheckValue = Double.parseDouble(value);
			if (dCheckValue instanceof Double == false) {
				return false;
			}
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 登录用户和密码check
	 * 只能包含字母和数字和特殊字符（~!@#$%^&*?）
	 * @param str
	 * @return
	 */
	public static boolean chkLoginUserPw(String str) {
		if (StringUtils.isNotBlank(str)) {
			char[] chars = str.toCharArray();
			int okCnt = 0;
			for (char c : chars) {
				// 字母
				if (Character.isDigit(c)) {
					okCnt++;
				} else if (Character.isUpperCase(c) || Character.isLowerCase(c)) {
					okCnt++;
				} else {
					if ("_".indexOf(c) > -1) {
						okCnt++;
					}
				}
			}
			return okCnt == chars.length;
		}
		return false;
	}
	
	/**
	 * 是否是连续字符
	 * @param str
	 * @return
	 */
	public static boolean isContinueChar(String str) {
		if (StringUtils.isNotBlank(str)) {
			char[] chars = str.toCharArray();
			if (chars.length > 1) {
				for (int i = 1; i < chars.length; i++) {
					if (Math.abs(chars[i] - chars[i - 1]) != 1) {
						return false;
					}
				}
			}
		}
		return true;
	}

	/**
	 * 校验空数组
	 * @param target
	 * @return
	 */
	public static boolean isEmptyList(List<?> target) {
		return null == target || target.isEmpty();
	}

	/**
	 * 检验空map
	 * @param target
	 * @return
	 */
	public static boolean isEmptyMap(Map<?, ?> target) {
		return null == target || target.isEmpty();
	}

	/**
	 * BigDecimal比较
	 * @param decimal1 第一个数
	 * @param decimal2 第二个数
	 * @return 比较结果 true: 第一个数大, false: 第二个数大
	 */
	public static boolean compareBigDecimal(BigDecimal decimal1, BigDecimal decimal2) {
		if (null == decimal1) {
			return false;
		}
		if (null == decimal2) {
			return true;
		}
		return decimal1.compareTo(decimal2) > 0;
	}

	/**
	 *  获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
	 * @param request
	 * @return
	 */
	public static String getIpAddress(HttpServletRequest request) {

		String ip = request.getHeader("X-Forwarded-For");

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("X-Real-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
		} else if (ip.length() > 15) {
			String[] ips = ip.split(",");
			for (int index = 0; index < ips.length; index++) {
				String strIp = (String) ips[index];
				if (!("unknown".equalsIgnoreCase(strIp))) {
					ip = strIp;
					break;
				}
			}
		}
		return ip;
	}
}